package com.zzy.python;

import org.junit.Test;
import org.python.core.Py;
import org.python.core.PyFunction;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.util.PythonInterpreter;

import com.alibaba.fastjson.JSON;

public class JythonDemo {

	@Test
	public void runDemo() {
		// 调用python的解释器
		PythonInterpreter interpreter = new PythonInterpreter();
		// 执行Python语句
		String codeStr = "str = 'hello world!  中国 '; ";
		PyString code = Py.newStringUTF8(codeStr);
		interpreter.exec(code);
		interpreter.exec("print(str);");
	}

	@Test
	public void runPy() {
		PythonInterpreter interpreter = new PythonInterpreter();
		// 我在这里使用相对路径，注意区分
		interpreter.execfile("D:\\python\\print_hello.py");
	}

	public static void main(String[] args) {
		String paramStr = "2023-12-11";
		PyString param = Py.newStringUTF8(paramStr);
		
		
		String path = "D:\\python\\now_func_no_main.py";
		PythonInterpreter interpreter = new PythonInterpreter();
		interpreter.execfile(path);
		PyFunction func = interpreter.get("get_day", PyFunction.class);
		PyObject pyobj = func.__call__(param);
		System.out.println(pyobj.toString());
        Object obj = JSON.parse(pyobj.toString());
        System.out.println(obj.toString());
	}
	

}
